package bjfuwjx.exp4;

import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

public class BallThread extends Thread {
    public CopyOnWriteArrayList<Ball> list1,list2;
    public MyPanel ballPanel;
    boolean IsStop=true;
    public BallThread(CopyOnWriteArrayList<Ball> list1, CopyOnWriteArrayList<Ball> list2, MyPanel ballPanel){
        this.list1=list1;
        this.list2=list2;
        this.ballPanel=ballPanel;
    }
    public Double[] RandomPostion(){
        Random random=new Random();
        Double[] position=new Double[2];
        Double[] px=new Double[2];
        px[0]=-40.0;
        px[1]=1000.0;
        int index= random.nextInt(2);
        position[0]=px[index];
        position[1]=700*random.nextDouble();
        return position;
    }
    @Override
    public void run() {
        while (true){
            Ball b=new Ball(RandomPostion());
            if (b.x==1000.0) {
                list1.add(b);
                ballPanel.add1(b);
            }
            else {
                list2.add(b);
                ballPanel.add2(b);
            }
            Random random=new Random();
            long sec=random.nextInt(1000);
            try {
                Thread.sleep(sec);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
